<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Because composite format strings are interpreted at runtime, rather than validated by the compiler, they can contain errors that lead to unexpected behaviors or runtime errors. This rule statically validates the good behavior of composite formats when calling the methods of <code>String.Format</code>, <code>StringBuilder.AppendFormat</code>, <code>Console.Write</code>, <code>Console.WriteLine</code>, <code>TextWriter.Write</code>, <code>TextWriter.WriteLine</code>, <code>Debug.WriteLine(String, Object[])</code>, <code>Trace.TraceError(String, Object[])</code>, <code>Trace.TraceInformation(String, Object[])</code>, <code>Trace.TraceWarning(String, Object[])</code> and <code>TraceSource.TraceInformation(String, Object[])</code>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_noncompliant_code_example">Noncompliant Code Example</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-cs" data-lang="cs">s = string.Format("{0}", arg0, arg1); // Noncompliant, arg1 is declared but not used.
s = string.Format("{0} {2}", arg0, arg1, arg2); // Noncompliant, the format item with index 1 is missing so arg1 will not be used.
s = string.Format("foo"); // Noncompliant, there is no need to use string.Format here.</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_compliant_solution">Compliant Solution</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-cs" data-lang="cs">s = string.Format("{0}", arg0);
s = string.Format("{0} {1}", arg0, arg2);
s = "foo";</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_exceptions">Exceptions</h2>
<div class="sectionbody">
<div class="ulist">
<ul>
<li>
<p>No issue is raised if the format string is not a <code>const</code>.</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre>var pattern = "{0} {1} {2}";
var res = string.Format(pattern, 1, 2); // Compliant, not const string are not recognized</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>No issue is raised if the argument is not an inline creation array.</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre>var array = new int[] {};
var res = string.Format("{0} {1}", array); // Compliant we don't know the size of the array</pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>This rule doesn&#8217;t check whether the format specifier (defined after the <code>:</code>) is actually valid.</p>
</li>
</ul>
</div>
<hr>
</div>
</div>
<div class="sect1">
<h2 id="_implementation_specification">Implementation Specification</h2>
<div class="sectionbody">
<div class="paragraph">
<p>(visible only on this page)</p>
</div>
<div class="sect2">
<h3 id="_message">Message</h3>
<div class="paragraph">
<p>XXXX</p>
</div>
<hr>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_comments_and_links">Comments And Links</h2>
<div class="sectionbody">
<div class="paragraph">
<p>(visible only on this page)</p>
</div>
<div class="sect2">
<h3 id="_is_duplicated_by_s3941">is duplicated by: <a data-rspec-id="S3941" class="rspec-auto-link">S3941</a></h3>

</div>
<div class="sect2">
<h3 id="_is_related_to_s2275">is related to: <a data-rspec-id="S2275" class="rspec-auto-link">S2275</a></h3>

</div>
<div class="sect2">
<h3 id="_on_10_dec_2015_090759_tamas_vajk_wrote">on 10 Dec 2015, 09:07:59 Tamas Vajk wrote:</h3>
<div class="paragraph">
<p>\[~ann.campbell.2] Removed the performance label, as the performance impact is insignificant.</p>
</div>
</div>
<div class="sect2">
<h3 id="_on_10_dec_2015_144405_ann_campbell_wrote">on 10 Dec 2015, 14:44:05 Ann Campbell wrote:</h3>
<div class="paragraph">
<p>I&#8217;ve updated SQALE characteristic to match [~tamas.vajk]</p>
</div>
</div>
</div>
</div>